package com.tofacebook.jdbc.service.impl;

import com.tofacebook.jdbc.maper.BookMapper;
import com.tofacebook.jdbc.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Override
    public void buyBook(Integer bookId, Integer userId) {
        //查询图书的价格
        Integer price = bookMapper.getPriceByBookId(bookId); //他们独占一个事务去执行sql语句的
        //更新图书的库存
        bookMapper.updateStock(bookId);
        //更新用户的余额
        bookMapper.updateBalance(userId, price);
    }
}
